// 支持的 AI 网站及其输入框选择器
const SUPPORTED_SITES = [
  {
    host: 'chat.openai.com',
    inputSelector: 'textarea',
  },
  {
    host: 'claude.ai',
    inputSelector: 'textarea',
  },
  {
    host: 'www.doubao.com',
    inputSelector: 'textarea',
  },
  {
    host: 'www.deepseek.com',
    inputSelector: 'textarea',
  },
];

let PROMPTS = [];

function getCurrentSiteConfig() {
  const host = window.location.host;
  return SUPPORTED_SITES.find(site => host.includes(site.host));
}

async function loadPrompts() {
  if (PROMPTS.length > 0) return PROMPTS;
  try {
    const res = await fetch(chrome.runtime.getURL('prompts.json'));
    PROMPTS = await res.json();
  } catch (e) {
    PROMPTS = [];
  }
  return PROMPTS;
}

async function showPromptPanelNearInput(input) {
  removePromptPanel();
  const rect = input.getBoundingClientRect();
  const panel = document.createElement('div');
  panel.id = 'ai-prompt-helper-panel';
  panel.style.position = 'fixed';
  panel.style.left = rect.left + 'px';
  panel.style.top = (rect.bottom + 6) + 'px';
  panel.style.width = rect.width + 'px';
  panel.style.background = '#fff';
  panel.style.border = '1px solid #ddd';
  panel.style.borderRadius = '8px';
  panel.style.boxShadow = '0 4px 16px rgba(0,0,0,0.12)';
  panel.style.padding = '12px 8px 8px 8px';
  panel.style.zIndex = 10000;

  const prompts = await loadPrompts();
  if (prompts.length === 0) {
    const empty = document.createElement('div');
    empty.textContent = '暂无可用提示词';
    empty.style.textAlign = 'center';
    empty.style.color = '#888';
    panel.appendChild(empty);
  } else {
    prompts.forEach(item => {
      const pBtn = document.createElement('button');
      pBtn.textContent = item.type + '：' + item.prompt.slice(0, 16) + (item.prompt.length > 16 ? '...' : '');
      pBtn.title = item.prompt;
      pBtn.style.display = 'block';
      pBtn.style.width = '100%';
      pBtn.style.margin = '8px 0';
      pBtn.style.padding = '8px';
      pBtn.style.background = '#f6f6f6';
      pBtn.style.border = 'none';
      pBtn.style.borderRadius = '6px';
      pBtn.style.cursor = 'pointer';
      pBtn.onclick = (e) => {
        e.stopPropagation();
        fillPromptToInput(input, item.prompt);
        removePromptPanel();
      };
      panel.appendChild(pBtn);
    });
  }

  document.body.appendChild(panel);

  // 点击外部关闭
  setTimeout(() => {
    document.addEventListener('mousedown', onClickOutside, { capture: true, once: true });
  }, 0);
}

function removePromptPanel() {
  const old = document.getElementById('ai-prompt-helper-panel');
  if (old) old.remove();
}

function onClickOutside(e) {
  const panel = document.getElementById('ai-prompt-helper-panel');
  if (panel && !panel.contains(e.target)) {
    removePromptPanel();
  }
}

function fillPromptToInput(input, text) {
  input.value = text;
  input.dispatchEvent(new Event('input', { bubbles: true }));
  input.focus();
}

(function main() {
  const site = getCurrentSiteConfig();
  if (!site) return;
  // 监听所有输入框的 focus 事件
  function bindInputs() {
    document.querySelectorAll(site.inputSelector).forEach(input => {
      if (!input._aiPromptBound) {
        input.addEventListener('focus', (e) => {
          showPromptPanelNearInput(input);
        });
        input._aiPromptBound = true;
      }
    });
  }
  bindInputs();
  // 监听 DOM 变化，动态页面也能绑定
  const observer = new MutationObserver(bindInputs);
  observer.observe(document.body, { childList: true, subtree: true });
})(); 